源码版本:paint.net 3.36 (https://github.com/rivy/OpenPDN), 后续的版本貌似不提供源码了
重要的几个工程目录:
Base: 基础数据结构, 和项目无关,例如Pair,Tuple,Set,EventArgs等
Core:和项目相关的数据结构,例如RendreArgs,Surface,Histogram,HsvColor
Data:项目文件类型,例如PdnFileType,JpegFileType等
paintdotnet:主入口,主逻辑
Effect:滤镜
SystemLayer:和系统相关, 例如ThreadBackground, Memory等,不是图层的Layer
Resource:cursor,icon,image等
启动入口:Startup类1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40StartPart2
{
Application.Run(this.mainForm);
}
internal sealed class MainForm: PdnBaseForm
{
private AppWorkspace appWorkspace;
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.defaultButton = new System.Windows.Forms.Button();
// 构造
this.appWorkspace = new PaintDotNet.AppWorkspace();
this.floaterOpacityTimer = new System.Windows.Forms.Timer(this.components);
this.deferredInitializationTimer = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// appWorkspace
//
this.appWorkspace.Dock = System.Windows.Forms.DockStyle.Fill;
this.appWorkspace.Location = new System.Drawing.Point(0, 0);
this.appWorkspace.Name = "appWorkspace";
this.appWorkspace.Size = new System.Drawing.Size(752, 648);
this.appWorkspace.TabIndex = 2;
this.appWorkspace.ActiveDocumentWorkspaceChanging += new EventHandler(AppWorkspace_ActiveDocumentWorkspaceChanging);
this.appWorkspace.ActiveDocumentWorkspaceChanged += new EventHandler(AppWorkspace_ActiveDocumentWorkspaceChanged);
//...
this.AutoScaleDimensions = new SizeF(96F, 96F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
this.ClientSize = new System.Drawing.Size(950, 738);
// 添加appWorkspace
this.Controls.Add(this.appWorkspace);
this.Controls.Add(this.defaultButton);
//...
}
}
AppWorkspace非常关键, 代码如下1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171internal class AppWorkspace: UserControl, ISnapObstacleHost
{
private AppEnvironment appEnvironment;
private DocumentWorkspace activeDocumentWorkspace;
// if a new workspace is added, and this workspace is not dirty, then it will be removed.
// This keeps track of the last workspace added via CreateBlankDocumentInNewWorkspace (if
// true was passed for its 2nd parameter)
private DocumentWorkspace initialWorkspace;
private List<DocumentWorkspace> documentWorkspaces = new List<DocumentWorkspace>();
private WorkspaceWidgets widgets;
//状态栏, 工具条, 悬浮窗
private Panel workspacePanel;
private PdnToolBar toolBar;
private PdnStatusBar statusBar;
private ToolsForm mainToolBarForm;
private LayerForm layerForm;
private HistoryForm historyForm;
private ColorsForm colorsForm;
//...
public AppWorkspace()
{
SuspendLayout();
// initialize!
InitializeComponent();
InitializeFloatingForms();
this.mainToolBarForm.ToolsControl.SetTools(DocumentWorkspace.ToolInfos);
this.mainToolBarForm.ToolsControl.ToolClicked += new ToolClickedEventHandler(this.MainToolBar_ToolClicked);
this.toolBar.ToolChooserStrip.SetTools(DocumentWorkspace.ToolInfos);
this.toolBar.ToolChooserStrip.ToolClicked += new ToolClickedEventHandler(this.MainToolBar_ToolClicked);
this.toolBar.AppWorkspace = this;
// init the Widgets container
this.widgets = new WorkspaceWidgets(this);
this.widgets.ViewConfigStrip = this.toolBar.ViewConfigStrip;
this.widgets.CommonActionsStrip = this.toolBar.CommonActionsStrip;
this.widgets.ToolConfigStrip = this.toolBar.ToolConfigStrip;
this.widgets.ToolsForm = this.mainToolBarForm;
this.widgets.LayerForm = this.layerForm;
this.widgets.HistoryForm = this.historyForm;
this.widgets.ColorsForm = this.colorsForm;
this.widgets.StatusBarProgress = this.statusBar;
this.widgets.DocumentStrip = this.toolBar.DocumentStrip;
// Load our settings and initialize the AppEnvironment
LoadSettings();
// hook into Environment *Changed events
AppEnvironment.PrimaryColorChanged += PrimaryColorChangedHandler;
AppEnvironment.SecondaryColorChanged += SecondaryColorChangedHandler;
AppEnvironment.ShapeDrawTypeChanged += ShapeDrawTypeChangedHandler;
AppEnvironment.GradientInfoChanged += GradientInfoChangedHandler;
AppEnvironment.ToleranceChanged += OnEnvironmentToleranceChanged;
AppEnvironment.AlphaBlendingChanged += AlphaBlendingChangedHandler;
AppEnvironment.FontInfo = this.toolBar.ToolConfigStrip.FontInfo;
AppEnvironment.TextAlignment = this.toolBar.ToolConfigStrip.FontAlignment;
AppEnvironment.AntiAliasingChanged += Environment_AntiAliasingChanged;
AppEnvironment.FontInfoChanged += Environment_FontInfoChanged;
AppEnvironment.FontSmoothingChanged += Environment_FontSmoothingChanged;
AppEnvironment.TextAlignmentChanged += Environment_TextAlignmentChanged;
AppEnvironment.PenInfoChanged += Environment_PenInfoChanged;
AppEnvironment.BrushInfoChanged += Environment_BrushInfoChanged;
AppEnvironment.ColorPickerClickBehaviorChanged += Environment_ColorPickerClickBehaviorChanged;
AppEnvironment.ResamplingAlgorithmChanged += Environment_ResamplingAlgorithmChanged;
AppEnvironment.SelectionCombineModeChanged += Environment_SelectionCombineModeChanged;
AppEnvironment.FloodModeChanged += Environment_FloodModeChanged;
AppEnvironment.SelectionDrawModeInfoChanged += Environment_SelectionDrawModeInfoChanged;
this.toolBar.DocumentStrip.RelinquishFocus += RelinquishFocusHandler;
this.toolBar.ToolConfigStrip.ToleranceChanged += OnToolBarToleranceChanged;
this.toolBar.ToolConfigStrip.FontAlignmentChanged += ToolConfigStrip_TextAlignmentChanged;
this.toolBar.ToolConfigStrip.FontInfoChanged += ToolConfigStrip_FontTextChanged;
this.toolBar.ToolConfigStrip.FontSmoothingChanged += ToolConfigStrip_FontSmoothingChanged;
this.toolBar.ToolConfigStrip.RelinquishFocus += RelinquishFocusHandler2;
this.toolBar.CommonActionsStrip.RelinquishFocus += OnToolStripRelinquishFocus;
this.toolBar.CommonActionsStrip.MouseWheel += OnToolStripMouseWheel;
this.toolBar.CommonActionsStrip.ButtonClick += CommonActionsStrip_ButtonClick;
this.toolBar.ViewConfigStrip.DrawGridChanged += ViewConfigStrip_DrawGridChanged;
this.toolBar.ViewConfigStrip.RulersEnabledChanged += ViewConfigStrip_RulersEnabledChanged;
this.toolBar.ViewConfigStrip.ZoomBasisChanged += ViewConfigStrip_ZoomBasisChanged;
this.toolBar.ViewConfigStrip.ZoomScaleChanged += ViewConfigStrip_ZoomScaleChanged;
this.toolBar.ViewConfigStrip.ZoomIn += ViewConfigStrip_ZoomIn;
this.toolBar.ViewConfigStrip.ZoomOut += ViewConfigStrip_ZoomOut;
this.toolBar.ViewConfigStrip.UnitsChanged += ViewConfigStrip_UnitsChanged;
this.toolBar.ViewConfigStrip.RelinquishFocus += OnToolStripRelinquishFocus;
this.toolBar.ViewConfigStrip.MouseWheel += OnToolStripMouseWheel;
this.toolBar.ToolConfigStrip.BrushInfoChanged += DrawConfigStrip_BrushChanged;
this.toolBar.ToolConfigStrip.ShapeDrawTypeChanged += DrawConfigStrip_ShapeDrawTypeChanged;
this.toolBar.ToolConfigStrip.PenInfoChanged += DrawConfigStrip_PenChanged;
this.toolBar.ToolConfigStrip.GradientInfoChanged += ToolConfigStrip_GradientInfoChanged;
this.toolBar.ToolConfigStrip.AlphaBlendingChanged += OnDrawConfigStripAlphaBlendingChanged;
this.toolBar.ToolConfigStrip.AntiAliasingChanged += DrawConfigStrip_AntiAliasingChanged;
this.toolBar.ToolConfigStrip.RelinquishFocus += OnToolStripRelinquishFocus;
this.toolBar.ToolConfigStrip.ColorPickerClickBehaviorChanged += ToolConfigStrip_ColorPickerClickBehaviorChanged;
this.toolBar.ToolConfigStrip.ResamplingAlgorithmChanged += ToolConfigStrip_ResamplingAlgorithmChanged;
this.toolBar.ToolConfigStrip.SelectionCombineModeChanged += ToolConfigStrip_SelectionCombineModeChanged;
this.toolBar.ToolConfigStrip.FloodModeChanged += ToolConfigStrip_FloodModeChanged;
this.toolBar.ToolConfigStrip.SelectionDrawModeInfoChanged += ToolConfigStrip_SelectionDrawModeInfoChanged;
this.toolBar.ToolConfigStrip.SelectionDrawModeUnitsChanging += ToolConfigStrip_SelectionDrawModeUnitsChanging;
this.toolBar.ToolConfigStrip.MouseWheel += OnToolStripMouseWheel;
this.toolBar.DocumentStrip.RelinquishFocus += OnToolStripRelinquishFocus;
this.toolBar.DocumentStrip.DocumentClicked += DocumentStrip_DocumentTabClicked;
this.toolBar.DocumentStrip.DocumentListChanged += DocumentStrip_DocumentListChanged;
// Synchronize
AppEnvironment.PerformAllChanged();
this.globalToolTypeChoice = this.defaultToolTypeChoice;
this.toolBar.ToolConfigStrip.ToolBarConfigItems = ToolBarConfigItems.None;
this.layerForm.LayerControl.AppWorkspace = this;
ResumeLayout();
PerformLayout();
}
private void InitializeFloatingForms()
{
// MainToolBarForm
mainToolBarForm = new ToolsForm();
mainToolBarForm.RelinquishFocus += RelinquishFocusHandler;
mainToolBarForm.ProcessCmdKeyEvent += OnToolFormProcessCmdKeyEvent;
// LayerForm
layerForm = new LayerForm();
layerForm.LayerControl.AppWorkspace = this;
layerForm.LayerControl.ClickedOnLayer += LayerControl_ClickedOnLayer;
layerForm.NewLayerButtonClick += LayerForm_NewLayerButtonClicked;
layerForm.DeleteLayerButtonClick += LayerForm_DeleteLayerButtonClicked;
layerForm.DuplicateLayerButtonClick += LayerForm_DuplicateLayerButtonClick;
layerForm.MergeLayerDownClick += new EventHandler(LayerForm_MergeLayerDownClick);
layerForm.MoveLayerUpButtonClick += LayerForm_MoveLayerUpButtonClicked;
layerForm.MoveLayerDownButtonClick += LayerForm_MoveLayerDownButtonClicked;
layerForm.PropertiesButtonClick += LayerForm_PropertiesButtonClick;
layerForm.RelinquishFocus += RelinquishFocusHandler;
layerForm.ProcessCmdKeyEvent += OnToolFormProcessCmdKeyEvent;
// HistoryForm
historyForm = new HistoryForm();
historyForm.RewindButtonClicked += HistoryForm_RewindButtonClicked;
historyForm.UndoButtonClicked += HistoryForm_UndoButtonClicked;
historyForm.RedoButtonClicked += HistoryForm_RedoButtonClicked;
historyForm.FastForwardButtonClicked += HistoryForm_FastForwardButtonClicked;
historyForm.RelinquishFocus += RelinquishFocusHandler;
historyForm.ProcessCmdKeyEvent += OnToolFormProcessCmdKeyEvent;
// ColorsForm
colorsForm = new ColorsForm();
colorsForm.PaletteCollection = new PaletteCollection();
colorsForm.WhichUserColor = WhichUserColor.Primary;
colorsForm.UserPrimaryColorChanged += ColorsForm_UserPrimaryColorChanged;
colorsForm.UserSecondaryColorChanged += ColorsForm_UserSecondaryColorChanged;
colorsForm.RelinquishFocus += RelinquishFocusHandler;
colorsForm.ProcessCmdKeyEvent += OnToolFormProcessCmdKeyEvent;
}
}
下图是Paint.Net的UI界面
是个悬浮窗分别是ToolsForm, LayerForm, HistoryForm, ColorsForm. 中间图像所在区域可以理解为DocumentWorkspace. 由于可以打开多张图像, 所以存在一个DocumentWorkspace列表. 当前正在处理的是activeDocumentWorkspace. 另外还有一个WorkspaceWidgets, 代码中将layerForm, historyForm, colorsForm, mainToolBarForm都赋值给了它